//
//  ExcelReader.m
//  ExcelToPlist
//
//  Created by 赵梁 on 2017/3/28.
//  Copyright © 2017年 赵梁. All rights reserved.
//

#import "ExcelReader.h"

#import "DHxlsReader.h"

@implementation ExcelReader

+ (NSArray *)JSONArrayWithExcelFilePath:(NSString *)filePath
{
    //  NSString *path = [[NSBundle mainBundle] pathForResource:filePath ofType:nil];
    //	NSString *path = @"/Volumes/Data/Users/dhoerl/Downloads/TestData.xls";
    //	NSString *path = @"/tmp/xls.xls";
    
    //xls_debug = 1; // good way to see everything in the Excel file
    
    DHxlsReader *reader = [DHxlsReader xlsReaderWithPath:filePath];
    if (!reader) {
        return nil;
    }
    
    /*
    NSString *text = @"";
    
    text = [text stringByAppendingFormat:@"AppName: %@\n", reader.appName];
    text = [text stringByAppendingFormat:@"Author: %@\n", reader.author];
    text = [text stringByAppendingFormat:@"Category: %@\n", reader.category];
    text = [text stringByAppendingFormat:@"Comment: %@\n", reader.comment];
    text = [text stringByAppendingFormat:@"Company: %@\n", reader.company];
    text = [text stringByAppendingFormat:@"Keywords: %@\n", reader.keywords];
    text = [text stringByAppendingFormat:@"LastAuthor: %@\n", reader.lastAuthor];
    text = [text stringByAppendingFormat:@"Manager: %@\n", reader.manager];
    text = [text stringByAppendingFormat:@"Subject: %@\n", reader.subject];
    text = [text stringByAppendingFormat:@"Title: %@\n", reader.title];
    
    text = [text stringByAppendingFormat:@"\n\nNumber of Sheets: %u\n", reader.numberOfSheets];
    
    */
    
    
    NSInteger numberOfCols = [reader numberOfColsInSheet:0];
    NSInteger numberOfRows = [reader numberOfRowsInSheet:0];
    
    
    if (numberOfRows<=0) {
        return nil;
    }
    
    NSInteger row = 1;
    NSInteger col = 1;
    
    
    /*
    NSMutableArray *keys = [NSMutableArray arrayWithCapacity:numberOfCols];
    for (col = 0; col<numberOfCols; col++) {
        DHcell *cell = [reader cellInWorkSheetIndex:0 row:row col:col];
        NSString *str = cell.str;
        if (!(str.length>0)) {
            str = [NSString stringWithCString:cell.colStr encoding:NSUTF8StringEncoding];
        }
        //区分
        if ([keys containsObject:str]) {
            str = [str stringByAppendingString:@"_"];
        }
        
        [keys addObject:str];
    }
    */
    
    NSMutableArray *retrunList = [NSMutableArray arrayWithCapacity:numberOfCols];
    
    for (row = 1; row<=numberOfRows; row++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:numberOfCols];
        [dic setValue:@(row) forKey:@"%row"];
        //[dic setValue:@(col) forKey:@"col"];
        for (col = 1; col<=numberOfCols; col++) {
            DHcell *cell = [reader cellInWorkSheetIndex:0 row:row col:col];
            NSString *str = [NSString stringWithFormat:@"%s",cell.colStr];
            assert(str);
            if (str.length==0) {
                continue;
            }
            [dic setValue:[NSString stringWithFormat:@"%@",cell.str] forKey:str];
        }
        [retrunList addObject:dic];
    }
    
    return retrunList;
}

@end
